//
//  MYNewsShopDetailsScenicspotMoreListVC.m
//  MuYing
//
//  Created by tbb on 2020/3/10.
//  Copyright © 2020 TBB. All rights reserved.
//

#import "MYNewsShopDetailsScenicspotMoreListVC.h"
#import "MYNewsShopDetailsModel.h"
#import "BRPickerView.h"
#import "MYNewsShopDetailsCalendar.h"
#import "MYNewsShopDetailsScenicspotOrderVC.h"
#import "MYNewsShopDetailsActionOrderVC.h"
@interface MYNewsShopDetailsScenicspotMoreListVC ()
//选种的规格
@property (nonatomic, strong) MYNewsShopDetailsStandardModel * standarModel;

@property (nonatomic, strong) NSDate * selectData;

@property (nonatomic, assign) NSInteger section;
@end

@implementation MYNewsShopDetailsScenicspotMoreListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kYQLS(@"选择类型");
    
}

- (void)setupView {
    [super setupView];
    kYQREGISTERTABLEVIEWCELL([MYNewsShopDetailsCategoryCell class])
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsShopDetailsTicketCell")
    if (self.isScenicspot) {
        self.section = 3;
    }else{
        self.section = 2;
    }
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    for (MYNewsShopDetailsStandardModel *standarModel in self.detailsModel.standardList) {
        if (standarModel.isCurrent) {
            self.standarModel = standarModel;
            break;
        }
    }
}

-(void)gotoPayVCTicketModel:(MYNewsShopDetailsStandardTicketModel *)ticketModel{
    
    if (!self.isScenicspot) {
        MYNewsShopDetailsActionOrderVC *payVC = [MYNewsShopDetailsActionOrderVC new];
        payVC.standardModel = self.standarModel;
        payVC.ticketModel = ticketModel;
        payVC.detailsModel = self.detailsModel;
        kYQPUSH(payVC);
    }
    
    if (!self.selectData) {
        kMBMesHUD(kYQLS(@"请选择日期"));
        return;
    }
    MYNewsShopDetailsScenicspotOrderVC *payVC = [MYNewsShopDetailsScenicspotOrderVC new];
    payVC.standardModel = self.standarModel;
    payVC.ticketModel = ticketModel;
    NSString *str1 = [NSDate getDateToString:self.selectData format:@"yyyy-MM-dd"];
    payVC.selectDateStr = str1;
    payVC.detailsModel = self.detailsModel;
    kYQPUSH(payVC);
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.section;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == self.section-1) {
        return self.standarModel.ticketList.count;
    }
    return 1;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MYNewsShopDetailsCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsShopDetailsCategoryCell"];
        cell.standardList = self.detailsModel.standardList;
        ws(self);
        cell.categorySelectedItemBlock = ^(NSInteger item) {
            MYNewsShopDetailsStandardModel * selectedStandardModel = nil;
            if (item < weakself.detailsModel.standardList.count) {
                selectedStandardModel = weakself.detailsModel.standardList[item];
                for (MYNewsShopDetailsStandardModel * standardModel in weakself.detailsModel.standardList) {
                    if (standardModel.standardId == selectedStandardModel.standardId) {
                        standardModel.isCurrent = YES;
                    }else{
                        standardModel.isCurrent = NO;
                    }
                }
                self.standarModel = selectedStandardModel;
                [weakself.listTableView reloadData];
            }
        };
        
        return cell;
    }else if (indexPath.section == self.section-1){
        
        MYNewsShopDetailsTicketCell *ticketCell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsShopDetailsTicketCell"];
        MYNewsShopDetailsStandardTicketModel *ticketModel = nil;
        if (indexPath.row < self.standarModel.ticketList.count) {
            ticketModel = self.standarModel.ticketList[indexPath.row];
        }
        ticketCell.ticketModel = ticketModel;
        ws(self);
        ticketCell.ticketBuyBlock = ^(MYNewsShopDetailsStandardTicketModel * ticketModel) {
            [weakself gotoPayVCTicketModel:ticketModel];
        };

        return ticketCell;
    }else{
        
        YQTableViewBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kTableViewCell"];
         if (!cell){
             cell = [[YQTableViewBaseCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"kTableViewCell"];
         }
         cell.contentView.backgroundColor = UIColor.whiteColor;
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
         cell.textLabel.text = kYQLS(@"选择日期");
         cell.detailTextLabel.text = kYQLS(@"请选择日期");
         cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
         cell.detailTextLabel.textColor = kAPPSubTitleColor;
         cell.textLabel.textColor = kAPPMainTitleColor;
         cell.textLabel.font = [UIFont systemFontOfSize:15 weight:(UIFontWeightMedium)];
         if (self.selectData) {
             NSString *str1 = [NSDate getDateToString:self.selectData format:@"MM月dd日"];
             cell.detailTextLabel.text = str1;
         }
         return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        NSInteger row = 0.0;
        if (self.detailsModel.standardList.count % 3 == 0) {
            row = self.detailsModel.standardList.count / 3;
        }else{
            row = self.detailsModel.standardList.count / 3 + 1;
        }
        return row * 28 + row* 8 + 56;
    }else if (indexPath.section == self.section-1) {
        return 56;
    }else{
        return 58;
    }

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && self.isScenicspot) {
        ws(self);
        [MYNewsShopDetailsCalendar showCalenderViewDate1:self.selectData date2:nil allowsMultipleSelection:NO selectDateBlock:^(NSDate * _Nonnull inCheckInDate, NSDate * _Nonnull leaveDate) {
            weakself.selectData = inCheckInDate;
            [weakself.listTableView reloadData];
        }];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
@end
